#include "stm32f1xx_hal.h"
#include "bsp_sys.h"

/** @defgroup BSP_SYS
	* @brief 系统配置
	* @{
	*/

/** \brief 配置系统时钟
  *
  *         The system Clock is configured as follow :\n
  *            System Clock source            = PLL (HSE)\n
  *            SYSCLK(Hz)                     = 72000000\n
  *            HCLK(Hz)                       = 72000000\n
  *            AHB Prescaler                  = 1\n
  *            APB1 Prescaler                 = 2\n
  *            APB2 Prescaler                 = 1\n
  *            HSE Frequency(Hz)              = 8000000\n
  *            HSE PREDIV1                    = 1\n
  *            PLLMUL                         = 9\n
  *            Flash Latency(WS)              = 0\n
  * \param None
  * \return None
  *
  */
void SystemClock_Config(void)
{
    RCC_ClkInitTypeDef clkinitstruct;
    RCC_OscInitTypeDef oscinitstruct;

    /* 开启外部高速时钟，PLL以外部时钟源激活 */
    oscinitstruct.OscillatorType    = RCC_OSCILLATORTYPE_HSE;
    oscinitstruct.HSEState          = RCC_HSE_ON;
    oscinitstruct.HSEPredivValue    = RCC_HSE_PREDIV_DIV1;
    oscinitstruct.PLL.PLLState      = RCC_PLL_ON;
    oscinitstruct.PLL.PLLSource     = RCC_PLLSOURCE_HSE;
    oscinitstruct.PLL.PLLMUL        = RCC_PLL_MUL9;
    if(HAL_RCC_OscConfig(&oscinitstruct)!=HAL_OK)
    {
        /* 时钟配置错误 */
        while(1);
    }

    /* 选择PLL作为系统时钟源，同时配置HCLK, PLCK1, PCLK2 时钟分频 */
    clkinitstruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
    clkinitstruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
    clkinitstruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
    clkinitstruct.APB2CLKDivider = RCC_HCLK_DIV1;
    clkinitstruct.APB1CLKDivider = RCC_HCLK_DIV2;
    if(HAL_RCC_ClockConfig(&clkinitstruct, FLASH_LATENCY_2)!=HAL_OK)
    {
        while(1);
    }
}

/**
  * @}
  */
